//
//  AddressUtil.swift
//  SlaughterClient
//
//  Created by Ken on 2020/3/28.
//  Copyright © 2020 zwb. All rights reserved.
//

import UIKit

class AddressUtil: NSObject {

}


public let provinceArr:[String] = {  // 省  列表
    let dic = getDictionaryFromJSONString(jsonString: getCityJson())
    let cityList = dic["citylist"] as! [NSDictionary]
    return cityList.compactMap { $0["p"] as? String }
}()
public let provinceFullNameArr:[String] = {  // 省  列表
    let dic = getDictionaryFromJSONString(jsonString: getCityJson(fullname: true))
    let cityList = dic["provice"] as! [NSDictionary]
    return cityList.compactMap { $0["name"] as? String }
}()

public let cityDic:[String:[String]] = {  // 市  列表
    let dic = getDictionaryFromJSONString(jsonString: getCityJson())
    let cityList = dic["citylist"] as! [NSDictionary]
    var cityDic = [String:[String]]()
    for item in cityList {
        let dicArr = item["c"] as! [NSDictionary]
        let arr = dicArr.compactMap { $0["n"] as? String }
        cityDic.updateValue(arr, forKey: item["p"] as! String)
    }
    return cityDic
    
}()
public let cityFullNameDic:[String:[String]] = {  // 市  列表
    let dic = getDictionaryFromJSONString(jsonString: getCityJson(fullname: true))
    let cityList = dic["provice"] as! [NSDictionary]
    var cityDic = [String:[String]]()
    for item in cityList {
        let dicArr = item["city"] as! [NSDictionary]
        let arr = dicArr.compactMap { $0["name"] as? String }
          cityDic.updateValue(arr, forKey: item["name"] as! String)
    }
    return cityDic
    
}()



public let areaDic:[String:[String]] = { //区  列表
    let dic = getDictionaryFromJSONString(jsonString: getCityJson())
    let cityList = dic["citylist"] as! [NSDictionary]
    var areaDic = [String:[String]]()
    for item in cityList {
        let dicArr = item["c"] as! [NSDictionary]
        for one in dicArr {
            if let a = one["a"] as? [NSDictionary] {
                let n = one["n"] as! String
                areaDic.updateValue(a.compactMap { $0["s"] as? String }, forKey: n)
            }
            
        }
    }
    return areaDic
    
}()

func getCityJson(fullname:Bool = false) -> String {
    //读取本地的文件
    
    var path = ""
    if fullname {
        path = Bundle.main.path(forResource: "city_fullname", ofType: "txt")!

    } else {
         path = Bundle.main.path(forResource: "city", ofType: "txt")!

    }
              let url = URL(fileURLWithPath: path)
             // 带throws的方法需要抛异常
             do {
                 /*
                  * try 和 try! 的区别
                  * try 发生异常会跳到catch代码中
                  * try! 发生异常程序会直接crash
                  */
              let data = try Data(contentsOf: url)
              let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
               let json = dictionaryToJsonBack(jsonData)
                return json.unwrap
                 
             } catch let error as Error? {
                 print("错误")
                return ""
             }
}
